package com.itcast.yygh.hosp.controller;

import com.itcast.common.exception.YyghException;
import com.itcast.common.result.R;
import com.itcast.common.utils.HttpRequestHelper;
import com.itcast.common.utils.MD5;
import com.itcast.common.utils.Result;
import com.itcast.yygh.hosp.service.DepartmentService;
import com.itcast.yygh.hosp.service.HospSetService;
import com.itcast.yygh.hosp.service.HospitalService;
import com.itcast.yygh.hosp.service.ScheduleService;
import com.itcast.yygh.model.model.hosp.Department;
import com.itcast.yygh.model.model.hosp.Hospital;
import com.itcast.yygh.model.model.hosp.Schedule;
import com.itcast.yygh.model.vo.hosp.DepartmentQueryVo;
import com.itcast.yygh.model.vo.hosp.ScheduleQueryVo;
import org.springframework.data.domain.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 医院平台调用的api接口
 */

@Api("医院平台接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Resource
    private HospitalService hospitalService;
    @Resource
    private HospSetService hospSetService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private ScheduleService scheduleService;

    //上传医院信息接口将医院信息保存到mongo中
    @ApiOperation("医院上传接口")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        //需要将上传医院信息中的sign进行校验
        //想要获取医院设置sign的值必须通过医院编码进行查询
        String hoscode = (String)map.get("hoscode");
        if(StringUtils.isEmpty(hoscode)){
            throw  new YyghException(20001,"失败");
        }
        //通过hoscode查询数据中的数据
        String signKey = hospSetService.getSignKey(hoscode);
        //加密
        String encrypt = MD5.encrypt(signKey);
        //获取上传医院接口中的signKey
        String sign = (String) map.get("sign");
        if(!encrypt.equals(sign)){
            throw new YyghException(20001,"检验失败");
        }
        //处理请求传过来的logoData的数据 将数据中的空格替换成+
        String logoData = (String) map.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        map.put("logoData",logoData);
        //需要通过查询医院设置的数据库中的sign值 加密后进行比对
        hospitalService.save(map);
        return Result.ok();
    }
    //显示医院平台医院的信息接口
    @ApiOperation("查询医院数据接口")
    @PostMapping("/hospital/show")
    public Result show(HttpServletRequest request){
        //1.获取请求中的map集合
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.校验医院编码
        String hoscode = (String)map.get("hoscode");
        if(StringUtils.isEmpty(hoscode)){
            throw  new YyghException(20001,"失败");
        }
        //通过hoscode查询数据中的数据
        /*String signKey = hospSetService.getSignKey(hoscode);
        //加密
        String encrypt = MD5.encrypt(signKey);
        //获取上传医院接口中的signKey
        String sign = (String) map.get("sign");
        if(!encrypt.equals(sign)){
            throw new YyghException(20001,"检验失败");
        }*/

        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);
    }
    //医院上传科室接口实现
    @ApiOperation("上传科室接口")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //1.先获取科室的map集合
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        //todo 2.获取医院接口传过来的sign_key的值和本系统库中的key进行校验

        //2.调用保存方法
        departmentService.save(map);
        return Result.ok();
    }
    //根据医院编号和部门编号查询科室的信息数据
    @ApiOperation("科室分页查询接口")
    @PostMapping("/department/list")
    public Result list(HttpServletRequest request){
        //1.获取请求中的对象map
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.获取map中部门编号和医院编号
        String hoscode = (String)map.get("hoscode");
        //3.将两个字段封装到查询对象中
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //4.获取当前页和每页显示记录数
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String)map.get("limit"));
        //5.调用查询分页的方法
        Page<Department> departmentPage = departmentService.findPage(page,limit,departmentQueryVo);
        return Result.ok(departmentPage);
    }
    //删除科室方法接口
    @ApiOperation("删除科室方法")
    @PostMapping("/department/remove")
    public Result remove(HttpServletRequest request){
        //1.格式化请求中的map数据
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.根据医院编号和科室编号删除数据 先获取医院编号和科室编号
        String hoscode = (String) map.get("hoscode");
        //科室编号
        String depcode = (String) map.get("depcode");
        //3.调用删除方法
        departmentService.remove(hoscode,depcode);
        //4.返回结果集
        return Result.ok();
    }
    //保存医院平台上传的排班数据
    @ApiOperation("保存排班数据")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //1.格式化请求中的map数据
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.todo 检验signkey的内容
        //3.执行保存方法
        scheduleService.save(map);
        //4.返回保存成功的消息
        return Result.ok();
    }
    //排班数据分页列表查询
    @ApiOperation("排班分页数据查询")
    @PostMapping("/schedule/list")
    public Result listPage(HttpServletRequest request){
        //1.格式化请求中的map数据
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.获取排班数据的查询条件医院编码
        String hoscode = (String) map.get("hoscode");
        //3.把查询条件设置到queryVo中
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        //4.处理map中的page和limit属性转成int类型
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String)map.get("limit"));
        //5.调用分页的查询方法
        Page<Schedule> schedulePage = scheduleService.findPage(page,limit,scheduleQueryVo);
        //6.返回查询到的数据
        return Result.ok(schedulePage);
    }
    //医院平台排班数据删除
    @ApiOperation("排班数据删除接口")
    @PostMapping("/schedule/remove")
    public Result removes(HttpServletRequest request){
        //1.格式化请求中的map数据
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.获取map中的医院编号和排班编号
        String hoscode = (String) map.get("hoscode");
        //排班编号
        String hosScheduleId = (String) map.get("hosScheduleId");
        try {
            //3.执行删除操作
            scheduleService.remove(hoscode,hosScheduleId);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
        //4.返回成功的结果集
        return Result.ok();
    }
}
